home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Utilities Professional 1-1500
/
Utilities Professional 1-1500 (1994)(WPD)[!].iso
/
12511500
/
var1451.dms
/
var1451.adf
/
ParallelDevice
/
Example1.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-04-27
|
15KB
|
401 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Parallel Device Tulevagen 22 */
/* File: Example1.c 181 41 LIDINGO */
/* Author: Anders Bjerin SWEDEN */
/* Date: 92-04-26 */
/* Version: 1.00 */
/* */
/* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
/* */
/* Registered members may use this program freely in their */
/* own commercial/noncommercial programs/articles. */
/* */
/***********************************************************/
/* This program demonstrates how you can use the Parallel Device. */
/* It does not do very much since I do not know what you have */
/* connected to your parallel port, but with small modifications */
/* you should be able to write your own parallel communication */
/* packages. */
#include <exec/types.h> /* STRPTR */
#include <devices/parallel.h> /* Parallel Device */
/* Size of our data buffer: */
#define MY_BUFFER_SIZE 200
/* Parallel flags: (Check for end-of-file characters.) */
#define PARALLEL_FLAGS PARF_EOFMODE
/* Additional flags: (Nothing) */
#define ADDITIONAL_FLAGS 0
/* Declare a pointer to our reply port: */
struct MsgPort *replymp = NULL;
/* Declare a pointer to our parallel request block: */
struct IOExtPar *parallel_req = NULL;
/* Store the parallel device error here: */
UWORD parallel_dever = TRUE;
/* Declare our data buffer: */
BYTE buffer[ MY_BUFFER_SIZE ];
/* Declare our functions: */
/* Our main function: */
void main();
/* Clears and removes everything nice and neatly: */
void clean_up( UBYTE error, STRPTR text );
UBYTE SetParParams(
struct IOExtPar *ioreq,
UBYTE parallel_flags,
ULONG extended_flags,
UBYTE *eof_chars
);
void ParError( UBYTE error );
UBYTE ParWrite(
struct IOExtPar *ioreq,
BYTE *data,
ULONG length
);
UBYTE ParRead(
struct IOExtPar *ioreq,
BYTE *data,
ULONG length
);
void main()
{
/* Error number: */
UBYTE error;
/* The eight end-of-file characters: */
/* They MUST be in descending order! */
UBYTE eof_char[8]={ 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00 };
/* Get a reply port: (No name, priority 0) */
replymp = (struct MsgPort *)
CreatePort( NULL, 0 );
if( !replymp )
clean_up( 0, "Could not create the reply port!" );
/* Create a parallel request block: */
parallel_req = (struct IOExtPar *)
CreateExtIO( replymp, sizeof( struct IOExtPar ) );
if( !parallel_req )
clean_up( 0, "Not enough memory for the parallel request block!" );
/* Open the Parallel Device: */
parallel_dever = OpenDevice( PARALLELNAME, 0, parallel_req, 0 );
if( parallel_dever )
clean_up( 0, "Could not open the Parallel Device!" );
/* Set the parallel device's parameters: */
error = (UBYTE) SetParParams(
parallel_req, /* Pointer to our parallel request block. */
PARALLEL_FLAGS, /* Parallel flags. */
ADDITIONAL_FLAGS, /* Additional flags. */
eof_char /* Pointer to an array of eight end-of-file chr. */
);
/* OK? */
if( error )
clean_up( error, "Could not set the parallel parameters!" );
/* Send 0 bytes to the parallel port: */
error = ParWrite( parallel_req, buffer, 0 );
if( error )
ParError( error );
else
printf( "Data has now been sent to the parallel port!\n" );
/* Collect 0 bytes from the parallel port: */
error = ParRead( parallel_req, buffer, 0 );
if( error )
ParError( error );
else
printf( "We have now collected some data from the paralle port!\n" );
/* Clean up and quit: */
clean_up( 0, "The End!" );
}
/* Close and return everything that has been */
/* opened and allocated before we quit: */
void clean_up( UBYTE error, STRPTR text )
{
/* Print some information about the problem: */
if( error )
ParError( error );
/* Close the Parallel Device: */
if( !parallel_dever )
CloseDevice( parallel_req );
/* Deallocate the parallel request block: */
if( parallel_req )
DeleteExtIO( parallel_req, sizeof( struct IOExtPar ) );
/* Remove the replyport: */
if( replymp )
DeletePort( replymp);
/* Print the message: */
printf( "\n%s\n", text );
/* Quit: */
exit( 0 );
}
/* SetParParams() sets the parallel parameters. It initializes a IOExtPar */
/* structure, and does a PDCMD_SETPARAMS commad. If everything is OK it */
/* returns NULL, else an error number is returned. */
/* */
/* Synopsis: er = SetParParams( io, bl, br, bt, rl, wl, sl, sf, ef, chr ); */
/* */
/* er: (UBYTE) SetParParams() returns 0 if everything was OK, else */
/* an error value is returned. See function ParError() for more */
/* information. */
/* */
/* io: (struct IOExtPar *) Pointer to the parallel request block you */
/* want to initialize. */
/* */
/* PARF_RAD_BOOGIE Not supported by the parallel device for the */
/* moment. */
/* */
/* PARF_SHARED Set this flag if you want to allow other */
/* tasks running at the same time to use the */
/* parallel device. The default is exclusive- */
/* access. (If some other task is using the */
/* parallel device with the shared bit set, and */
/* you call this function with exclusive access, */
/* your request will fail.) */
/* */
/* PARF_EOFMODE Set this flag if you want to check for end of */
/* file characters. (You may use up to eight end */
/* of file characters, which are specified */
/* below.) */
/* */
/* ef: (ULONG) Not supported by the parallel device for the moment. */
/* */
/* chr: (UBYTE *) Pointer to an array containing eight end-of-file */
/* characters. If the parallel flag "PARF_EOFMODE" is set, the */
/* parallel device will check each character which is sent or */
/* received, and if it matches one of the end-of-file characters */
/* the read/wite request is terminated. */
UBYTE SetParParams(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
UBYTE parallel_flags, /* Parallel flags. */
ULONG extended_flags, /* Additional parallel flags. */
UBYTE *eof_chars /* Pointer to an array containing eight end-of- */
/* file characters. */
)
{
int loop; /* Used in the loop. */
UBYTE *ptr; /* Unsigned byte pointer. */
/* Set parallel flags: */
ioreq->io_ParFlags = parallel_flags;
/* Set additional flags: */
ioreq->io_PExtFlags = extended_flags;
/* Get the address of the IOTArray: */
ptr = (UBYTE *) &(ioreq->io_PTermArray);
/* Set all eight end of file characters: */
for( loop=0; loop < 8; loop++ )
{
/* Copy character after character: */
*ptr = eof_chars[ loop ];
/* Step one byte foreward: */
ptr++;
}
/* All values have now been set, lets do a PDCMD_SETPARAMS request: */
ioreq->IOPar.io_Command = PDCMD_SETPARAMS;
/* Do our request, and when complete return 0 if */
/* OK, else an error value: */
return( (UBYTE) DoIO( ioreq ) );
}
/* ParError() tells the user what went wrong. You give it the error code */
/* you received, and ParError() will print a short description of the */
/* problem. Useful when debugging. */
/* */
/* Synopsis: ParError( error ); */
/* */
/* error: (UBYTE) The error value you want to have explained. */
void ParError( UBYTE error )
{
switch( error )
{
case ParErr_DevBusy:
printf( "Some other task is already using the parallel Device!\n" );
break;
case ParErr_BufTooBig:
printf( "The parallel buffer is too big! (?)\n" );
break;
case ParErr_InvParam:
printf( "Invalid parameters!\n" );
break;
case ParErr_LineErr:
printf( "Line error, check all cables!\n" );
break;
case ParErr_NotOpen:
printf( "The Parallel Device is not open!\n" );
break;
case ParErr_PortReset:
printf( "Someone has resetted the Parallel Device!\n" );
break;
case ParErr_InitErr:
printf( "Could not initialize the Parallel Device!\n" );
break;
default:
printf( "An unknown error was reported! Error nr: %d\n", error );
}
}
/* ParWrite() sends some data to the Parallel Port. You only have */
/* to give it a pointer to the data you want to write and tell it */
/* how many bytes you want to transfer. */
/* */
/* Synopsis: error = ParWrite( io, data, length ); */
/* */
/* error: (UBYTE) ParWrite() returns 0 if everything was OK, */
/* else an error number is returned. */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized */
/* parallel request block. */
/* */
/* data: (BYTE *) Pointer to the first byte of the data you */
/* want to send (write). */
/* */
/* length: (ULONG) How many bytes you want to transfer. If you */
/* want to continue to send data until we have received */
/* an end-of-file character, set the length to -1. */
/* (Note that the parallel device will then ONLY stop */
/* when it has received one of the end-of-file */
/* characters.) */
UBYTE ParWrite(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
BYTE *data, /* Pointer to the data you want to send. */
ULONG length /* The length of the data you want to send. */
)
{
/* We want to send (write) some data: */
ioreq->IOPar.io_Command = CMD_WRITE;
/* Give the start address of our data: */
ioreq->IOPar.io_Data = (APTR) data;
/* Set the length of the message: (If you want to continue */
/* to write until you have received an end-of-file character, */
/* set length to -1.) */
ioreq->IOPar.io_Length = length;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: (This is a task sleep.) */
return( (UBYTE) DoIO( ioreq ) );
}
/* ParRead() reads some data from the Parallel Port. You only have */
/* to give it a pointer to some memory where the data should be */
/* stored, and tell it how many bytes you want to read. The rest is */
/* done automatically. */
/* */
/* Synopsis: error = ParRead( io, data, length ); */
/* */
/* error: (UBYTE) ParRead() returns 0 if everything was OK, else */
/* an error number is returned. */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized parallel */
/* request block. */
/* */
/* data: (BYTE *) Pointer to the memory buffer where you want */
/* to store all collected data. */
/* */
/* length: (ULONG) How many bytes you want to read. If you want to */
/* continue to read data until we have received an end-of- */
/* file character, set the length to -1. */
UBYTE ParRead(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
BYTE *data, /* Where the data should be placed. */
ULONG length /* How many bytes you want to read. */
)
{
/* We want to read some data: */
ioreq->IOPar.io_Command = CMD_READ;
/* Give the start address of our data: */
ioreq->IOPar.io_Data = (APTR) data;
/* Set how many bytes you want to read. (If you want to continue */
/* to read data until you have received an end-of-file character, */
/* set length to -1.) */
ioreq->IOPar.io_Length = length;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: (This is a task sleep. Zzz ) */
return( (UBYTE) DoIO( ioreq ) );
}